package services;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import pd.SwissSMSClient;

public class Sunrise implements SMSService {

	private static final String host = "https://mip.sunrise.ch";
	private String authcookie = "";
	private String authcookie2 = "";
	
	public boolean doLogin(String userid, String password) {
		String postheader = "POST /mip/dyn/login/login?SAMLRequest=fVLJTsMwEL0j8Q%2BW79kqQGA1qUoRohJL1AYO3FxnkrpNxsHjtPD3pGnLcoCjn5%2FfMp7h6L2u2AYsaYMxj%2FyQM0Blco1lzJ%2BzW%2B%2BSj5LTkyHJumrEuHVLnMFbC%2BRY9xJJ9Bcxby0KI0mTQFkDCafEfPxwLwZ%2BKBprnFGm4mx6E3OZy2YtscG11qpeQYFqoXGdr81KQYmrCosiL%2FOas5djrMEu1pSohSmSk%2Bg6KAyvvDDywsssOhPhhTg%2Ff%2BUsPThda9w3%2BC%2FWYk8icZdlqZc%2BzbNeYKNzsI8dO%2BalMWUFvjL1zj6VRHrTwc62wNmYCKzr8k0MUluDnYPdaAXPs%2FuYL51rSATBdrv1v1UCGVCLVlN3WgZSEU%2F6wYq%2Bm%2F0x0f%2BTy6M1T77Fh8EPqeTwYbse05vUVFp9sHFVme3EgnRfJW6NraX72y3yox7RuVf0VNEiNaB0oSHnLEj2rr83o9uXTw%3D%3D&RelayState=https%3A%2F%2Fwww.google.com%2Fa%2Fsunrise.ch%2FServiceLogin%3Fcontinue%3Dhttp%253A%252F%252Fpartnerpage.google.com%252Fsunrise.ch%252Fdefault%252Fpostlogin%253Fpid%253Dsunrise.ch%2526url%253Dhttp%253A%252F%252Fpartnerpage.google.com%252Fsunrise.ch%26followup%3Dhttp%253A%252F%252Fpartnerpage.google.com%252Fsunrise.ch%252Fdefault%252Fpostlogin%253Fpid%253Dsunrise.ch%2526url%253Dhttp%253A%252F%252Fpartnerpage.google.com%252Fsunrise.ch%26service%3Dig%26passive%3Dtrue%26cd%3DUS%26hl%3Dde%26nui%3D1%26ltmpl%3Ddefault%26go%3Dtrue%26passive_sso%3Dtrue HTTP/1.1\r\n" +
		"Host: mip.sunrise.ch\r\n" +
		"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5\r\n" +
		"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
		"Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n" +
		"Accept-Encoding: gzip,deflate\r\n" +
		"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n" +
		"Keep-Alive: 300\r\n" +
		"Connection: close\r\n" +
		"Referer: https://mip.sunrise.ch/mip/dyn/login/login?SAMLRequest=fVLJTsMwEL0j8Q%2BW79kqQGA1qUoRohJL1AYO3FxnkrpNxsHjtPD3pGnLcoCjn5%2FfMp7h6L2u2AYsaYMxj%2FyQM0Blco1lzJ%2BzW%2B%2BSj5LTkyHJumrEuHVLnMFbC%2BRY9xJJ9Bcxby0KI0mTQFkDCafEfPxwLwZ%2BKBprnFGm4mx6E3OZy2YtscG11qpeQYFqoXGdr81KQYmrCosiL%2FOas5djrMEu1pSohSmSk%2Bg6KAyvvDDywsssOhPhhTg%2Ff%2BUsPThda9w3%2BC%2FWYk8icZdlqZc%2BzbNeYKNzsI8dO%2BalMWUFvjL1zj6VRHrTwc62wNmYCKzr8k0MUluDnYPdaAXPs%2FuYL51rSATBdrv1v1UCGVCLVlN3WgZSEU%2F6wYq%2Bm%2F0x0f%2BTy6M1T77Fh8EPqeTwYbse05vUVFp9sHFVme3EgnRfJW6NraX72y3yox7RuVf0VNEiNaB0oSHnLEj2rr83o9uXTw%3D%3D&RelayState=https%3A%2F%2Fwww.google.com%2Fa%2Fsunrise.ch%2FServiceLogin%3Fcontinue%3Dhttp%253A%252F%252Fpartnerpage.google.com%252Fsunrise.ch%252Fdefault%252Fpostlogin%253Fpid%253Dsunrise.ch%2526url%253Dhttp%253A%252F%252Fpartnerpage.google.com%252Fsunrise.ch%26followup%3Dhttp%253A%252F%252Fpartnerpage.google.com%252Fsunrise.ch%252Fdefault%252Fpostlogin%253Fpid%253Dsunrise.ch%2526url%253Dhttp%253A%252F%252Fpartnerpage.google.com%252Fsunrise.ch%26service%3Dig%26passive%3Dtrue%26cd%3DUS%26hl%3Dde%26nui%3D1%26ltmpl%3Ddefault%26go%3Dtrue%26passive_sso%3Dtrue\r\n" +
		"Content-Type: application/x-www-form-urlencoded\r\n" +
		"Content-Length: ";
		
		String postbody = "";
		try {
			postbody = "username=" + URLEncoder.encode(userid, "UTF8") + "&password=" + URLEncoder.encode(password, "UTF8") + "&_remember=on";
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		}

		String post = postheader + postbody.length() + "\r\n\r\n" + postbody;
		
		String response = SwissSMSClient.sendSSL(host, post);
		
		String strsetcookie1 = "Set-Cookie: JSESSIONID=";
		int coopos = response.indexOf(strsetcookie1);
		int delpos = response.indexOf("\r\n\r\n");
		
		if(coopos == -1 || delpos == -1 || coopos > delpos){
			return false;
		}
		
		authcookie = response.substring(coopos + strsetcookie1.length(), delpos);
		authcookie = authcookie.substring(0, authcookie.indexOf(';'));
		
		String strsetcookie2 = "Set-Cookie: SMIP=";
		coopos = response.indexOf(strsetcookie2);
		delpos = response.indexOf("\r\n\r\n");
		
		if(coopos == -1 || delpos == -1 || coopos > delpos){
			return false;
		}
		
		authcookie2 = response.substring(coopos + strsetcookie2.length(), delpos);
		authcookie2 = authcookie2.substring(0, authcookie2.indexOf(';'));
		
		return true;
	}

	public boolean sendMessage(String number, String message) {		
		try {
			message = new String(message.getBytes("UTF-8"), "ISO-8859-1");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return false;
		}
		String postHeader = "POST /mip/dyn/sms/sms?up_contactsPerPage=6&lang=de&country=us&.lang=de&.country=us&synd=ig&mid=14&ifpctok=-5330933670422295110&parent=http://partnerpage.google.com&libs=xB-WoatiXJY/lib/libcore.js,4aEaFcUuV9I/lib/libdynamic-height.js&extern_js=/extern_js/f/CgJlbhICdXMrMBA4ACw/gCQhJE1wsLw.js HTTP/1.1\r\n" +
						"Host: mip.sunrise.ch\r\n" +
						"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; de; rv:1.9.0.5) Gecko/2008120122 Firefox/3.0.5\r\n" +
						"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
						"Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n" +
						"Accept-Encoding: gzip,deflate\r\n" +
						"Accept-Charset: <utf-8;q=0.7,*;q=0.7\r\n" +
						"Keep-Alive: 300\r\n" +
						"Connection: close\r\n" +
						"Referer: https://mip.sunrise.ch/mip/dyn/sms/sms?up_contactsPerPage=6&lang=de&country=us&.lang=de&.country=us&synd=ig&mid=14&ifpctok=-5330933670422295110&parent=http://partnerpage.google.com&libs=xB-WoatiXJY/lib/libcore.js,4aEaFcUuV9I/lib/libdynamic-height.js&extern_js=/extern_js/f/CgJlbhICdXMrMBA4ACw/gCQhJE1wsLw.js\r\n" +
						"Cookie:  JSESSIONID=" + authcookie + ";SMIP=" + authcookie2 + "; org.springframework.web.servlet.i18n.CookieLocaleResolver.LOCALE=de; mip_msg_dispContacts=0\r\n" +
						"Content-Type: multipart/form-data; boundary=---------------------------41184676334\r\n" +
						"Content-Length: ";
		String postBody = 	"-----------------------------41184676334\r\n" +
						"Content-Disposition: form-data; name=\"recipient\"\r\n" +
						"\r\n" +
						number + "\r\n" +
						"-----------------------------41184676334\r\n" +
						"Content-Disposition: form-data; name=\"charsLeft\"\r\n" +
						"\r\n" +
						"150 / 1\r\n" +
						"-----------------------------41184676334\r\n" +
						"Content-Disposition: form-data; name=\"type\"\r\n" +
						"\r\n" +
						"sms\r\n" +
						"-----------------------------41184676334\r\n" +
						"Content-Disposition: form-data; name=\"message\"\r\n" +
						"\r\n" +
						message + "\r\n" +
						"-----------------------------41184676334\r\n" +
						"Content-Disposition: form-data; name=\"mmsAttachment\"; filename=\"\"\r\n" +
						"Content-Type: application/octet-stream\r\n" +
						"\r\n" +
						"\r\n" +
						"-----------------------------41184676334\r\n" +
						"Content-Disposition: form-data; name=\"mmsAttachmentFileName\"\r\n" +
						"\r\n" +
						"\r\n" +
						"-----------------------------41184676334\r\n" +
						"Content-Disposition: form-data; name=\"send\"\r\n" +
						"\r\n" +
						"send\r\n" +
						"-----------------------------41184676334\r\n" +
						"Content-Disposition: form-data; name=\"task\"\r\n" +
						"\r\n" +
						"send\r\n" +
						"-----------------------------41184676334--";
		
		String post = postHeader + postBody.length() + "\r\n\r\n" + postBody;
		SwissSMSClient.sendSSL(host, post);
		return true;
	}
}
